from models.student import Student
stus = []

def show_op():
    print('\n选择操作：添加A, 删除D, 更新U, 查询工号Q, 查询姓名N, 列表L, 退出E')

def getemp(nu):
    for e in emps:
        if e['no'] == nu:
            return e
    return None

def getempn(name):
    arr = []
    for e in emps:
        if e['name'].find(name) != -1:
            arr.append(e)
    return arr

def addemp():
    nu = int(input("输入学号"))
    em = getemp(nu)
    if em:
        print("学生已存在！")
        return
    name = input("输入姓名")
    age = int(input("输入年龄"))
    ob = input("输入专业")
    emps.append({'no': nu, 'name': name, 'age': age, 'major': ob})
    print('添加成功')

def delemp():
    nu = int(input("输入要删除的学号"))
    em = getemp(nu)
    if em:
        emps.remove(em)
        print('删除成功！')
    else:
        print('学生不存在！')

def searchemp():
    n = int(input('输入查询方式：1:按学号查询，2:按姓名查询'))
    if n == 1:
        nu = int(input('输入要查询的学号'))
        em = getemp(nu)
        if em:
            print(f_title.format('学号', '姓名', '年龄', '专业'))
            print(f_data.format(em['no'], em['name'], em['age'], em['major'], em['salary']))
        else:
            print("学生不存在！")
    elif n == 2:
        name = input('输入要查询的姓名')
        l = getempn(name)
        if l:
            print(f_title.format('工号', '姓名', '年龄', '岗位', '薪水'))
            for e in l:
                print(f_data.format(e['no'], e['name'], e['age'], e['major'], e['salary']))
        else:
            print('员工不存在！')
    else:
        print('请选择正确操作！')

def changeemp():
    nu = int(input('输入要更改的工号'))
    em = getemp(nu)
    if em:
        emps.remove(em)
        name = input("输入姓名")
        age = int(input("输入年龄"))
        ob = input("输入岗位")
        mo = int(input("输入薪水"))
        emps.append({'no': nu, 'name': name, 'age': age, 'major': ob, 'salary': mo})
        print('更改成功！')
    else:
        print("员工不存在！")

def listemp():
    if not emps:
        print("暂无员工信息")
        return
    print(f_title.format('工号', '姓名', '年龄', '岗位', '薪水'))
    for e in emps:
        print(f_data.format(e['no'], e['name'], e['age'], e['major'], e['salary']))


def run():
    print('welcome')
    op = input('输入')
    op = op.upper()
    if op == 'A':
        addemp()
    elif op == 'D':
        delemp()
    elif op == 'U':
        changeemp()
    elif op == 'Q':
        searchemp()
    elif op == 'N':
        searchemp_by_name()
    elif op == 'L':
        listemp()
    elif op == 'E':
        print("bye")
        break
    else:
        print('请选择正确操作！')